package com.ruoyi.web.controller.shop.shop.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.controller.mall.mallShopUser.mapper.MallShopUserMapper;
import com.ruoyi.web.controller.mall.orderOrder.domain.OrderOrder;
import com.ruoyi.web.controller.shop.shop.domain.ShopShop;
import com.ruoyi.web.controller.shop.shop.dto.ShopDto;
import com.ruoyi.web.controller.shop.shop.mapper.ShopShopMapper;
import com.ruoyi.web.controller.shop.shop.service.IShopShopService;
import com.ruoyi.web.utils.uniquekey.IdGererateFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 店铺信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-15
 */
@Service
public class ShopShopServiceImpl implements IShopShopService 
{
    @Resource
    private IdGererateFactory idGererateFactory;

    @Autowired
    private ShopShopMapper shopShopMapper;

//    @Autowired
//    private ShopMoneyMapper shopMoneyMapper;
    @Autowired
    private MallShopUserMapper mallShopUserMapper;

    /**
     * 查询店铺信息
     * 
     * @param id 店铺信息ID
     * @return 店铺信息
     */
    @Override
    public ShopShop selectShopShopById(String id)
    {
        return shopShopMapper.selectShopShopById(id);
    }

    /**
     * 查询店铺信息列表
     * 
     * @param shopShop 店铺信息
     * @return 店铺信息
     */
    @Override
    public List<ShopShop> selectShopShopList(ShopShop shopShop)
    {
        return shopShopMapper.selectShopShopList(shopShop);
    }

    /**
     * 新增店铺信息
     * 
     * @param shopShop 店铺信息
     * @return 结果
     */
    @Override
    public int insertShopShop(ShopShop shopShop)
    {
        shopShop.setId(this.idGererateFactory.nextStringId());
        return shopShopMapper.insertShopShop(shopShop);
    }

    /**
     * 修改店铺信息
     * 
     * @param shopShop 店铺信息
     * @return 结果
     */
    @Override
    public int updateShopShop(ShopShop shopShop)
    {
        return shopShopMapper.updateShopShop(shopShop);
    }

    @Override
    @Transactional
    public AjaxResult changeShopShop(ShopShop shopShop) {
        shopShopMapper.updateShopShop(shopShop);
        Map<String,Object> map = new HashMap<>();
        map.put("id",shopShop.getId());
        map.put("address",shopShop.getAddress());
        map.put("introduce",shopShop.getIntroduce());
        map.put("phone",shopShop.getPhone());
        map.put("startAt",shopShop.getStartAt());
        map.put("subheading",shopShop.getSubheading());
        map.put("img",shopShop.getImg());
        mallShopUserMapper.changeShopShop(map);
        return AjaxResult.success();
    }

    /**
     * 批量删除店铺信息
     * 
     * @param ids 需要删除的店铺信息ID
     * @return 结果
     */
    @Override
    public int deleteShopShopByIds(String[] ids)
    {
        return shopShopMapper.deleteShopShopByIds(ids);
    }

    /**
     * 删除店铺信息信息
     * 
     * @param id 店铺信息ID
     * @return 结果
     */
    @Override
    public int deleteShopShopById(String id)
    {
        return shopShopMapper.deleteShopShopById(id);
    }

    @Override
    public List<ShopDto> findShopListByMap(HashMap<String, Object> map) {
        return this.shopShopMapper.findShopListByMap(map);
    }

    @Override
    public List<ShopShop> findListByUid(Long uid,Double lng,Double lat) {
        return this.shopShopMapper.findListByUid(uid,lng,lat);
    }

    @Override
    public List findSonIds(String parentId) {
        return this.shopShopMapper.findSonIds(parentId);
    }

    @Override
    public void updateByReturnOrder(OrderOrder orderOrder) {
        //修改分店余额 销量
        ShopShop shopShop = this.shopShopMapper.selectShopShopById(orderOrder.getShopId());
        ShopShop updateShop = new ShopShop();
        updateShop.setId(shopShop.getId());
        updateShop.setSale(shopShop.getSale()-1);
//        updateShop.setMoney(shopShop.getMoney().add(orderOrder.getAllPrice()));
//        updateShop.setAllMoney(shopShop.getAllMoney().subtract(orderOrder.getAllPrice()));
        this.shopShopMapper.updateShopShop(updateShop);

        //修改总店余额
        ShopShop parentShop = this.shopShopMapper.selectShopShopById(shopShop.getParentId());
        ShopShop updateParent = new ShopShop();
        updateParent.setId(parentShop.getId());
//        updateParent.setMoney(parentShop.getMoney().add(orderOrder.getAllPrice()));
//        updateParent.setAllMoney(parentShop.getAllMoney().subtract(orderOrder.getAllPrice()));
        updateParent.setSale(parentShop.getSale()-1);
        this.shopShopMapper.updateShopShop(updateParent);
    }

}
